package com.gsafety.wellsfort.service.anyineng;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.gsafety.wellsfort.common.context.LoginContextUtil;
import com.gsafety.wellsfort.common.util.ApiResponse;
import com.gsafety.wellsfort.common.util.ResultCode;
import com.gsafety.wellsfort.dao.anyineng.AnYiNengConfDAO;
import com.gsafety.wellsfort.dao.anyineng.AnYiNengDAO;
import com.gsafety.wellsfort.dao.anyineng.AnyinengOpreateLogDAO;
import com.gsafety.wellsfort.dao.course.StudentExamDAO;
import com.gsafety.wellsfort.dao.knowledge.ExamPaperDAO;
import com.gsafety.wellsfort.domain.dto.PageQueryDTO;
import com.gsafety.wellsfort.domain.dto.course.MyExamDTO;
import com.gsafety.wellsfort.domain.model.anyineng.AnYiNeng;
import com.gsafety.wellsfort.domain.model.anyineng.AnYiNengConf;
import com.gsafety.wellsfort.domain.model.anyineng.AnyinengOpreateLog;
import com.gsafety.wellsfort.domain.model.course.StudentExam;
import com.gsafety.wellsfort.domain.model.knowledge.ExamPaper;
import com.gsafety.wellsfort.domain.vo.AnYiNengConfVO;
import com.gsafety.wellsfort.domain.vo.AnYiNengVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

@Service
@Slf4j
public class AnYiNengService {
    @Autowired(required = false)
    AnYiNengDAO anYiNengDAO;
    @Autowired(required = false)
    AnYiNengConfDAO anYiNengConfDAO;
    @Autowired(required = false)
    AnyinengOpreateLogDAO anyinengOpreateLogDAO;
    @Autowired(required = false)
    ExamPaperDAO examPaperDAO;
    @Autowired(required = false)
    StudentExamDAO studentExamDAO;
    public ApiResponse<AnYiNengConfVO> saveConfig(AnYiNengConfVO anYiNengConfVO){
        ApiResponse<AnYiNengConfVO> apiResponse = null;
        AnYiNengConf anYiNengConf =new AnYiNengConf();
        BeanUtils.copyProperties(anYiNengConfVO,anYiNengConf);
        if(anYiNengConf.getId()!=null){
            anYiNengConf.setUpdateTime(new Date());
            anYiNengConf.setModified(LoginContextUtil.getAdmin().getId());
            anYiNengConfDAO.updateByPrimaryKeySelective(anYiNengConf);
        }
        apiResponse = ApiResponse.success(anYiNengConfVO);
        return apiResponse;
    }
    public ApiResponse<AnYiNengConfVO> getConfig()  {
        ApiResponse<AnYiNengConfVO> apiResponse = null;
        AnYiNengConf anYiNengConf=anYiNengConfDAO.selectAnYiNengConf();
        AnYiNengConfVO anYiNengConfVO= AnYiNengConfVO.builder().build();
        BeanUtils.copyProperties(anYiNengConf,anYiNengConfVO);
        if(anYiNengConf.getAnyinengExamId()!=null){
            ExamPaper examPaper=examPaperDAO.selectByPrimaryKey(anYiNengConf.getAnyinengExamId());
            anYiNengConfVO.setAnyinengExamPaper(examPaper);
        }
        apiResponse = ApiResponse.success(anYiNengConfVO);
        return apiResponse;
    }
    public ApiResponse<AnYiNengVO> myAnYiNeng()  {
        ApiResponse<AnYiNengVO> apiResponse = null;
        Integer studentId=LoginContextUtil.getStudent().getId();
        Integer clsId=LoginContextUtil.getStudent().getClassId();
        AnYiNengVO anYiNengVO=AnYiNengVO.builder().build();
        if(studentId!=null){
            AnYiNeng anYiNeng=anYiNengDAO.selectByStudentId(studentId);
            if(anYiNeng==null){
                anYiNeng=new AnYiNeng();
                //0代表未激活,1代表已激活
                anYiNeng.setState((byte)0);
                anYiNeng.setStudentId(studentId);
                anYiNeng.setCreateTime(new Date());
                anYiNeng.setCreator(studentId);
                anYiNeng.setVal(BigDecimal.valueOf(500));
                anYiNengDAO.insertSelective(anYiNeng);
                BeanUtils.copyProperties(anYiNeng,anYiNengVO);

            }else{
                BeanUtils.copyProperties(anYiNeng,anYiNengVO);
            }

            AnYiNengConf anYiNengConf=anYiNengConfDAO.selectAnYiNengConf();
            ExamPaper anyinengPaper;
            if(anYiNengConf.getAnyinengExamId()!=null){
                anYiNengVO.setAnyinengPaperId(anYiNengConf.getAnyinengExamId());
                anyinengPaper=examPaperDAO.selectByPrimaryKey(anYiNengConf.getAnyinengExamId());
                //先判断是否有安意能试卷,安意能不需要发布记录,但需要往学生试卷里插入一条关联安意能的试卷。
                MyExamDTO myExamDTOParam =new MyExamDTO();
                myExamDTOParam.setStudentId(studentId);
                myExamDTOParam.setExamId(anyinengPaper.getId());
                myExamDTOParam.setExamPublishId(-1);
                List<StudentExam> stuExamList=studentExamDAO.selectByCondition(myExamDTOParam);
                if(stuExamList.size()<=0){
                    StudentExam stuExam =new StudentExam();
                    stuExam.setClassId(clsId);
                    stuExam.setStudentId(studentId);
                    stuExam.setFinishState(Byte.parseByte("0"));
                    stuExam.setPublishTime(new Date());
                    stuExam.setExamId(anyinengPaper.getId());
                    stuExam.setExamPublishId(-1);
                    studentExamDAO.insertSelective(stuExam);
                }
                anYiNengVO.setExamDetail(anyinengPaper);
            }


        }
        apiResponse = ApiResponse.success(anYiNengVO);
        return apiResponse;
    }
    public ApiResponse<AnYiNengVO> myAnYiNengDetail(PageQueryDTO pageQueryDTO)  {
        ApiResponse<AnYiNengVO> apiResponse = null;
        Integer studentId=LoginContextUtil.getStudent().getId();
        Integer clsId=LoginContextUtil.getStudent().getClassId();
        AnYiNengVO anYiNengVO=AnYiNengVO.builder().build();
        if(studentId!=null){
            AnYiNeng anYiNeng=anYiNengDAO.selectByStudentId(studentId);
            AnYiNengConf anYiNengConf=anYiNengConfDAO.selectAnYiNengConf();
            if(anYiNengConf.getAnyinengExamId()!=null){
                anYiNengVO.setAnyinengPaperId(anYiNengConf.getAnyinengExamId());
            }

            BeanUtils.copyProperties(anYiNeng,anYiNengVO);
            Page page= PageHelper.startPage(pageQueryDTO.getPageNo(), pageQueryDTO.getPageSize());
            List<AnyinengOpreateLog> anyinengOpreateLogList= anyinengOpreateLogDAO.selectByAnYiNengId(anYiNeng.getId());
            PageInfo pageInfo= new PageInfo(page);
            pageInfo.setList(anyinengOpreateLogList);
            anYiNengVO.setAnyinengOpreateLogList(anyinengOpreateLogList);


        }
        apiResponse = ApiResponse.success(anYiNengVO);
        return apiResponse;
    }
    public ApiResponse<ExamPaper> getAnYiNengExamPapaer(){
        ApiResponse<ExamPaper> apiResponse = null;
        ExamPaper anyinengPaper;
        AnYiNengConf anYiNengConf=anYiNengConfDAO.selectAnYiNengConf();
        if(anYiNengConf.getAnyinengExamId()!=null){
            anyinengPaper=examPaperDAO.selectByPrimaryKey(anYiNengConf.getAnyinengExamId());
            apiResponse = ApiResponse.success(anyinengPaper);
        }else{
            apiResponse=ApiResponse.error(ResultCode.ERROR,"未配置安意能！");
        }
        return apiResponse;
    }
}
